import os

# 指定要遍历的目标文件夹
target_directory = r"C:\Users\user\Downloads\vant-demo_master_vant_vite_\vant\vite\src"  # 你可以修改为你的目标目录
output_file = "output.txt"  # 输出文件
allowed_extensions = {".md", ".tsx", ".ts", ".vue", ".json",".js"}  # 允许的文件扩展名

# 打开 output.txt 进行写入
with open(output_file, "w", encoding="utf-8") as out_f:
    # 遍历文件夹
    for root, dirs, files in os.walk(target_directory):
        for file in files:
            file_path = os.path.join(root, file)  # 获取完整路径
            if os.path.splitext(file)[1] in allowed_extensions:  # 检查文件扩展名
                try:
                    # 读取文件内容
                    with open(file_path, "r", encoding="utf-8") as in_f:
                        content = in_f.read()

                    # 写入 output.txt，并添加标题信息
                    out_f.write(f"====== 文件: {file_path} ======\n")
                    out_f.write(content + "\n\n")
                    out_f.write("=" * 50 + "\n\n")  # 添加分隔线，方便阅读

                    print(f"已收集: {file_path}")
                except Exception as e:
                    print(f"跳过 {file_path}，原因: {e}")

print(f"\n所有文件内容已收集到 {output_file}！")